package com.hjl.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjl.Main;
import com.hjl.pojo.Headline;
import com.hjl.pojo.vo.PortalVo;
import com.hjl.service.HeadlineService;
import com.hjl.mapper.HeadlineMapper;
import com.hjl.utils.JwtHelper;
import com.hjl.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86176
 * @description 针对表【news_headline】的数据库操作Service实现
 * @createDate 2024-08-21 21:25:42
 */
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline> implements HeadlineService {
    @Autowired
    private HeadlineMapper headlineMapper;
    @Autowired
    private JwtHelper jwtHelper;


    /**
     * 分页查询首页头条信息
     * 1、进行分页数据查询
     * 2、分页数据，拼接到result即可
     * 注意1:查询需要自定义语句自定义mapper的方法，带分页
     * 注意2:返回的结果List<Map>
     *
     * @param portalVo
     * @return
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {
//        分页查询
        //Page->当前页数 页容量
        IPage<Map> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        headlineMapper.selectMyPage(page, portalVo);
//        结果封装
//        List<Map> records = page.getRecords();
        //将需要的数据封装到dataInfo集合里
        Map dataInfo = new HashMap();
        dataInfo.put("pageData", page.getRecords());//对应的数据集合
        dataInfo.put("pageNum", page.getCurrent());//页码
        dataInfo.put("pageSize", page.getSize());//页容量
        dataInfo.put("totalPage", page.getPages());//总页数
        dataInfo.put("totalSize", page.getTotal());//总条数

        //将上面dataInfo封装到data集合里
        Map data = new HashMap();
        data.put("pageInfo", dataInfo);
        return Result.ok(data);
    }

    /**
     * 查看头条全文详情
     *
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(int hid) {
        //根据id查询信息  多表联查
        Map headline = headlineMapper.selectMapInfo(hid);
        Map data = new HashMap();
        data.put("headline", headline);

        //2.拼接头条对象(阅读量和version)进行数据更新
        Headline headline1 = new Headline();
        headline1.setHid(hid);
        headline1.setPageViews((Integer) headline.get("pageViews") + 1);//阅读量加1
        headline1.setVersion((Integer) headline.get("version"));//设置版本
        headlineMapper.updateById(headline1);

        return Result.ok(data);
    }

    /**
     * 发布内容
     *
     * @param headline
     * @param token    获取userid 查询发布者
     * @return
     */
    @Override
    public Result publish(Headline headline, String token) {
        //根据token获取id
        int userId = jwtHelper.getUserId(token).intValue();


        //装配数据
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        headlineMapper.insert(headline);
        return Result.ok(null);
    }

    /**
     * 修改数据
     * 1.根据hid查询最新version
     * 2.修改更新时间
     * @param headline
     * @return
     */
    @Override
    public Result updateData(Headline headline) {
        //根据hid查询最新version
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();

        headline.setVersion(version);//乐观锁
        headline.setUpdateTime(new Date());

        headlineMapper.updateById(headline);
        return Result.ok(null);
    }
}




